package com.klitors.creator.utils;

import java.lang.reflect.Field;

/**
 * @author 66
 * @date 2016年10月8日
 */
public class CreateUtils {
	/*public static String getPath(Class<?> clazz){
		return "src/"+clazz.getPackage().getName().replace(".", "/");
	}*/
	public static String getParentPath(Class<?> clazz,String sourcePath){
		return sourcePath+"/"+getParentPackage(clazz).replace(".", "/");
	}
	public static String getBeanPath(Class<?> clazz,String sourcePath){
		return sourcePath+"/"+clazz.getPackage().getName().replace(".", "/");
	}
	public static String getParentPackage(Class<?> clazz){
		String name = clazz.getPackage().getName();
		return name.substring(0,name.lastIndexOf("."));
	}
	public static String getPackage(String path){
		return path.substring(path.indexOf("/")+1).replace("/", ".");
	}
	
	public static String firstCharLower(String str){
		return str.substring(0, 1).toLowerCase()+str.substring(1);
	}
	public static String getIdType(Class<?> clazz){
		Field id = getId(clazz);
		if(id==null) {
			return "Long";
		}
		String fullType = id.getType().getName();
		String id_type = fullType.substring(fullType.lastIndexOf(".")+1);
		return id_type;
	}
	public static String getIdName(Class<?> clazz){
		Field id = getId(clazz);
		if(id == null) {
			return "id";
		}
		return id.getName();
	}
	private static Field getId(Class<?> clazz){
		Field[] dfs = clazz.getDeclaredFields();
		if(dfs.length == 0) {
			return null;
		}
		Field df = dfs[0];
		return df;
	} 
	public static String classifyType(Class<?> type){
		String name = type.getName();
		if(type.isPrimitive()){
			return "base";
		}
		if(name.substring(0,name.indexOf(".")).equals("java")){
			//是java中的类
			String java_ = name.substring(name.indexOf(".")+1);
			String util_ = java_.substring(0, java_.indexOf("."));
			if("util".equals(util_)){
				//是集合
				return "set";
			}else{
				//是普通类
				return "common";
			}
		}else{
			//是自己创建的类
			return "self";
		}
	}
	
	public static String getGenericType(String gt){
		String sb = gt.substring(gt.indexOf("<")+1);
		String fullName = sb.substring(0, sb.indexOf(">"));
		
		return fullName;
		
	}
	public static String getSimpleName(String gt){
		
		return gt.substring(gt.lastIndexOf(".")+1);
		
	}
	
}
